package com.huilan.hgt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huilan.hgt.entity.Role;
import com.huilan.hgt.entity.User;
import com.huilan.hgt.service.RoleService;
import com.huilan.hgt.service.UserRoleService;
import com.huilan.hgt.service.UserService;
import com.huilan.hgt.util.Constants;
import com.huilan.hgt.util.MD5Util;
import com.huilan.hgt.util.PageUtil;
import com.huilan.hgt.util.ResponseResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ghy
 * @since 2018-11-02
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Autowired
    UserRoleService userRoleService;


    @GetMapping(value = "/list.do")
    public String userList(){
        return "user/user_list";
    }

    /**
     * @param user
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/queryList.do")
    public PageUtil queryList(PageUtil<User> page, User user ){

        page = userService.queryUserList(page,user);
        return page;
    }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/delete.do")
    public ResponseResult delete(User user) {
        boolean flag = userService.removeById(user.getUserId());
        return ResponseResult.success(flag);
    }

    /**
     * 跳转到修改编辑页面
     * @param modelMap
     * @param user
     * @return
     */
    @GetMapping(value = "/view.do")
    public String viewUser(ModelMap modelMap,User user){

        //查询所有角色信息
        List<Role> roleList = roleService.list(null);

        if(user.getUserId()!=null){
            user = userService.getById(user.getUserId());

            Integer roleId = userRoleService.getRoleByUserId(user.getUserId());

            modelMap.put("user",user);
            modelMap.put("roleId",roleId);

        }
        modelMap.put("roleList",roleList);

        return "user/user_view";
    }


    /**
     * 管理员设置用户信息
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/update.do")
    public ResponseResult update(User user,String roleId){
        boolean flag = userService.saveOrUpdate(user,roleId);
        return ResponseResult.success(flag);
    }


    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/updateUser.do")
    public ResponseResult setUser(User user){
        if(StringUtils.isNotEmpty(user.getPassword())){
            user.setPassword(MD5Util.encryption(user.getPassword()));
        }
        boolean flag = userService.updateById(user);
        return ResponseResult.success(flag);
    }




    /**
     * 我的信息
     */
    @GetMapping(value = "/myDetails.do")
    public String myDetails(ModelMap modelMap){
        User user = Constants.getCurrentUser();
        user = userService.getById(user.getUserId());
        modelMap.put("user",user);
        return "/user/my_details";
    }
}

